package com.wits.qso.web.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.Servlets;

import com.wits.platform.common.utils.PageUtil;
import com.wits.platform.web.controller.BaseController;
import com.wits.platform.web.mvc.PageParam;
import com.wits.qso.common.enumerate.BusinessType;
import com.wits.qso.entity.Aid;
import com.wits.qso.entity.Btable;
import com.wits.qso.entity.Cls;
import com.wits.qso.entity.Course;
import com.wits.qso.entity.CourseMine;
import com.wits.qso.entity.CreContent;
import com.wits.qso.entity.HomeworkH;
import com.wits.qso.entity.MaterialD;
import com.wits.qso.entity.Student;
import com.wits.qso.service.AttachmentService;
import com.wits.qso.service.BtableDaoService;
import com.wits.qso.service.ClsService;
import com.wits.qso.service.CourseMineService;
import com.wits.qso.service.CourseService;
import com.wits.qso.service.CreContentService;
import com.wits.qso.service.StudentService;

/**
 * 我的课程
 * @author admin
 *
 */
@Controller
@RequestMapping(value = "qso/coursemine")
public class CourseMineController extends BaseController {
	
	@Autowired
	private CourseMineService courseMineService;
	@Autowired
	private BtableDaoService btableDaoService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClsService clsService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private CreContentService creContentService;
	@Autowired
	private AttachmentService attachmentService;
	
	@RequestMapping(value = "/toList")
	public String toList() {
		return "qso/coursemine/list";
	}
	/**
	 * 加载我的课程列表页面
	 * 
	 * @param model
	 * @param page
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public String list(Model model, @ModelAttribute("page") PageParam page, HttpServletRequest req) {
		Map<String, Object> params = Servlets.getParametersStartingWith( req, "search_" );
		String userId=getCurrentUserId();
		Student stu= studentService.findByUserId(userId);
		Cls claId=clsService.getOne(stu.getClsId());
		List<Btable> btables=btableDaoService.findByclaId(claId);
		Set<String> set=new HashSet<String>();
		Map<String, Object> teas=new HashMap<String, Object>();
		for(int i=0;i<btables.size();i++){
			Btable bt=btables.get(i);
			if(!set.contains(bt.getCouid().getId())){
				set.add(bt.getCouid().getId());
				teas.put(bt.getCouid().getId(), bt.getTeaid().getTerName());
			}
		}
		params.put("courseIds", set.toArray());
		params.put("teas", teas);
		Page<CourseMine> list=courseMineService.findCourseMineViewPages(params, page);
		
		return PageUtil.toAjaxPageResult(list);
	}
	
	@RequestMapping(value = "/view/view")
	public String toView(Model model, @RequestParam String id) {
		if (StringUtils.isNotBlank( id )) {
			String userId=getCurrentUserId();
			Student stu= studentService.findByUserId(userId);
			Cls claId=clsService.getOne(stu.getClsId());
			Course course=courseService.getOne(id);
			List<Btable> btables=btableDaoService.findByclaIdAndCourseId(claId, course);
			Btable btable=btables.get(0);
			Aid aid = creContentService.findAidByBtable(btable);
			CreContent creContent = creContentService.findCreContentByBtable( btable );
			HomeworkH homeworkH = creContentService.findHomeworkHByBtable( btable );
			model.addAttribute( "aid", null == aid ? null : aid );
			model.addAttribute( "creContent", null == creContent ? null : creContent );
			model.addAttribute( "homeworkH", null == homeworkH ? null : homeworkH );
			List<MaterialD> materialDs = creContentService.findMaterialDByBtable( btable );
			model.addAttribute( "materialDs", null == materialDs ? null : materialDs );			 			
			model.addAttribute( "aidAttachments", null == aid ? null : attachmentService.findByBusiness( aid.getId(),  BusinessType.AID.getValue() ) );
			model.addAttribute( "homeworkHAttachments", null == homeworkH ? null :attachmentService.findByBusiness( homeworkH.getId(),  BusinessType.HWKH.getValue() ) );

			model.addAttribute( "btable", btable );
		}
		return "qso/coursemine/form";
	}

}
